package com.tablefood.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.tablefood.domain.ShopCategory;
import com.tablefood.domain.ShopProduct;
import com.tablefood.service.CategoryService;
import com.tablefood.service.ProductService;
import com.tablefood.utils.PageProductBean;
import com.tablefood.utils.ResultUtils;

public class ProductAction extends ActionSupport {


  @Resource
  private ProductService productService; 
  
  @Resource
  private CategoryService categoryService; 
  
  private PageProductBean pageProductBean; 
  private List<ShopProduct> listProduct;
  private int page = 1; 
  private ShopProduct product;
  private long productid;
  private String message;
  private String search;
  
  //用户搜索某一商品
  public String Search() throws Exception{
	 
	  this.pageProductBean = productService.searchForPage(search, 5, page);
	  this.listProduct = this.pageProductBean.getList();
	  System.out.println(listProduct.size());
	  return this.SUCCESS;
  }
   
  // 显示商品表
  public String productList() throws Exception {
    this.pageProductBean = productService.queryForPage(5, page);// 获取封装了分页信息和数据的pageBean
    this.listProduct = this.pageProductBean.getList(); // 获取数据
    return SUCCESS;
  }
    
  // 删除商品-更改销售状态
  public String productDel() throws Exception {
    // System.out.println(userid);
    if (productid == 0) {
      this.setMessage("删除失败");
      return ERROR;
    }

    if (productService.delProduct(productid)) {
      this.pageProductBean = productService.queryForPage(5, page);// 获取封装了分页信息和数据的pageBean
      this.listProduct = this.pageProductBean.getList(); // 获取数据
      this.setMessage("删除成功");
      return SUCCESS;
    } else {
      this.setMessage("删除失败");
      return ERROR;
    }
  }
    
  // 商品详细信息
  public String productDetail() throws Exception {

    if (productid == 0) {
      product = null;
      return SUCCESS;
    }
     
    this.product =  productService.searchProducts(productid);

    if (this.product != null) {
    	return SUCCESS;
    }
    return ERROR;
  }
    

  //添加一个商品
  public String productAdd() throws Exception {

    // 获取post请求的数据
    Map<String, String> param = new HashMap<String, String>();
    HttpServletRequest request = ServletActionContext.getRequest();
    Map<String, String[]> params = request.getParameterMap();
    for (String key : params.keySet()) {
      String[] values = params.get(key);
      for (int i = 0; i < values.length; i++) {
        param.put(key, values[i]);

      }
    }

    Map<String, Object> map = new HashMap<String, Object>();
    if (param.get("id") == "" || param.get("cateid") == "") {
      map.put("status", -1);
      ResultUtils.toJson(ServletActionContext.getResponse(), map);
      return null;
    }
    
    
    // 检索名字有没有重复
    if(productService.checkProductTitle(param.get("title"))){
      map.put("status", -1);
      ResultUtils.toJson(ServletActionContext.getResponse(), map);
      return null;
    }

    // 查找相关类型
   	Long cateid = Long.parseLong(param.get("cateid"));
   	ShopCategory category = categoryService.getCategoryByCateId(cateid);
     if(category == null){
     	map.put("status", -1);
         ResultUtils.toJson(ServletActionContext.getResponse(), map);
         return null;
     }
     
     // 实例化
     ShopProduct product = new ShopProduct();
     product.setShopCategory(category);
     product.setTitle(param.get("title"));
     product.setDescr(param.get("descr"));
     product.setNum(Long.parseLong(param.get("num")));
     product.setPrice(Double.parseDouble(param.get("price")));
     product.setCover(param.get("cover"));
     product.setIssale(param.get("issale"));
     product.setSaleprice(Double.parseDouble(param.get("saleprice")));
     product.setIshot(param.get("ishot"));
     product.setIson(param.get("ison"));
     product.setIstui(param.get("istui"));
     product.setState("1");
    
    if(productService.addProduct(product)){
      map.put("status", 0);
    }else{
      map.put("status", -1);
    }

    ResultUtils.toJson(ServletActionContext.getResponse(), map);
    return null;
  }
    
  //修改商品
  public String productUpdate() throws Exception {
	  
    // 获取post请求的数据
    Map<String, String> param = new HashMap<String, String>();
    HttpServletRequest request = ServletActionContext.getRequest();
    Map<String, String[]> params = request.getParameterMap();
    for (String key : params.keySet()) {
      String[] values = params.get(key);
      for (int i = 0; i < values.length; i++) {
        param.put(key, values[i]);

      }
    }

    Map<String, Object> map = new HashMap<String, Object>();
    if (param.get("id") == "" || param.get("cateid") == "") {
      map.put("status", -1);
      ResultUtils.toJson(ServletActionContext.getResponse(), map);
      return null;
    }
    
    this.productid = Long.parseLong(param.get("id"));
    ShopProduct product = productService.searchProducts(this.productid);
    
  
    // 检索名字有没有重复
    if(productService.checkProductTitle(param.get("title")) && !product.getTitle().equals(param.get("title")) ){
      map.put("status", -1);
      ResultUtils.toJson(ServletActionContext.getResponse(), map);
      return null;
    }
    

  	// 查找相关类型
  	Long cateid = Long.parseLong(param.get("cateid"));
  	ShopCategory category = categoryService.getCategoryByCateId(cateid);
    if(category == null){
    	map.put("status", -1);
        ResultUtils.toJson(ServletActionContext.getResponse(), map);
        return null;
    }
    
    // 实例化
    product.setShopCategory(category);
    product.setTitle(param.get("title"));
    product.setDescr(param.get("descr"));
    product.setNum(Long.parseLong(param.get("num")));
    product.setPrice(Double.parseDouble(param.get("price")));
    product.setCover(param.get("cover"));
    product.setIssale(param.get("issale"));
    product.setSaleprice(Double.parseDouble(param.get("saleprice")));
    product.setIshot(param.get("ishot"));
    product.setIson(param.get("ison"));
    product.setIstui(param.get("istui"));
    
    if(productService.updateProduct(product)){
      map.put("status", 0);
    }else{
      map.put("status", -1);
    }

    ResultUtils.toJson(ServletActionContext.getResponse(), map);
    return null;
  }
  
  public PageProductBean getPageProductBean() {
    return pageProductBean;
  }
  public void setPageProductBean(PageProductBean pageProductBean) {
    this.pageProductBean = pageProductBean;
  }
  public List<ShopProduct> getListProduct() {
    return listProduct;
  }
  public void setListProduct(List<ShopProduct> listProduct) {
    this.listProduct = listProduct;
  }
  public int getPage() {
    return page;
  }
  public void setPage(int page) {
    this.page = page;
  }
  public ShopProduct getProduct() {
    return product;
  }
  public void setProduct(ShopProduct product) {
    this.product = product;
  }
  public long getProductid() {
    return productid;
  }
  public void setProductid(long productid) {
    this.productid = productid;
  }
  public String getMessage() {
    return message;
  }
  public void setMessage(String message) {
    this.message = message;
  }


	public String getSearch() {
		return search;
	}
	
	public void setSearch(String search) {
		this.search = search;
	}
  
}
